#include "Ray_Util.h"
#include <glut.h>



int window_width = 400, window_height = 300;
int size=window_width*window_height;
Engine* raytracer;
bool finished=false;

void update();


void display()
{
	finished=raytracer->Render();
	float* pixels = new float[size*3];
	Pixel* buffer=raytracer->GetScreen()->GetScreenBuffer();

	for(int i=0,j=0;i<size*3;i+=3,j++) {

		pixels[i]=buffer[j].red;
		pixels[i+1]=buffer[j].green;
		pixels[i+2]=buffer[j].blue;
	} 

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glDrawPixels(window_width,window_height,GL_RGB,GL_FLOAT,pixels);

	glutSwapBuffers();

	if (!finished)
	{
		update();
	}
}

void update() {
	glutPostRedisplay();
}

int main(int argc, char** argv) {
	raytracer=new Engine(window_width,window_height,-4,4,-3,3);
	

	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(window_width, window_height);
	glutCreateWindow("Ray Tracing");

	glutDisplayFunc(display);

	glutMainLoop();
}